<?php
// +----------------------------------------------------------------------
// | Created by [ PhpStorm ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 上海到啦网络科技有限公司.
// +----------------------------------------------------------------------
// | Create Time ( 2017/8/22 0022 - 下午 4:57)
// +----------------------------------------------------------------------
// | Author: tangyijun <251784425@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\extra\General;
use app\extra\Sql;

/**
 * Class Admin
 * @package app\admin\controller
 * 用户操作类
 */
class User extends Base{
    private $table_name = 'user';
    /**
     * @return \think\response\View
     * 系统管理员列表
     */
    public function userList(){
        $param = [
            'username'=>'like'
        ];
        $search = General::search($param,'a.id > 0');
        $list = Sql::_select($this->table_name,$search['where'],[['lvshi b','a.id=b.user_id','left']],'a.*,b.*');
        return view('userList',[
            'list' => $list,
            'search' => $search['search']
        ]);
    }

    /**
     * @return array|\think\response\Json|\think\response\View
     * 系统管理员修改和添加
     */
    public function cu(){
        if(request()->isPost()){
            $post = request()->post();
            $data['username'] = $post['username'];
            $data['password'] = password_hash($post['password'],true);
            $data['email'] = $post['email'];
            $data['mobile'] = $post['mobile'];
            $data['type'] = 2;
            $data['time'] = time();
            $user_id = Sql::_save('user',$data);
            if($user_id > 0) {
                $data1['user_id'] = $user_id;
                $data1['name'] = $post['name'];
                $data1['workfirmname'] = $post['workfirmname'];
                $data1['code'] = $post['code'];
                $data1['qq'] = $post['qq'];
                $data1['adds'] = $post['adds'];
                $data1['work'] = $post['work'];
                $res = Sql::_save('lvshi', $data1);
            }
            if($res){
                return formatSuccessResult();
            }else{
                return formatResult($res['errorMsg'],10020);
            }
        }
        return view('cu1');
    }

    /**
     * @return \think\response\View
     * 重置管理员密码
     */
    public function resetPwd(){
        if(request()->isPost()){
            $post = request()->post();
            $where['id']=$post['id'];
            $data['password'] = password_hash($post['password'],true);
            $res = Sql::_setField('user',$where,$data);
            if($res){
                return formatSuccessResult();
            }else{
                return formatResult($res['errorMsg'],10020);
            }
        }
        $id = intval(input('param.id'));
        $one = Sql::_find($this->table_name,['id' => $id]);
        return view('resetPwd',[
            'one' => $one
        ]);
    }
}